package com.bawei.fuxi.ui.login;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bawei.fuxi.R;
import com.bawei.fuxi.base.mvp.BaseActivity;
import com.bawei.fuxi.entity.LoginEntity;
import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

import java.util.HashMap;

@Route(path = "/app/login")
public class LoginActivity extends BaseActivity<LoginContract.ILoginView,LoginPresenter> implements LoginContract.ILoginView {
    private EditText etUsername;
    private EditText etPassword;
    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }
    @Override
    public void initView(Bundle savedInstanceState) {
        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
    }

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void initData() {

    }
    //登陆结果
    @Override
    public void showLogin(LoginEntity loginEntity) {
        if(loginEntity != null && loginEntity.getCode() == 200){
            Toast.makeText(this, "mvp登陆成功", Toast.LENGTH_SHORT).show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    EMClient.getInstance().logout(true);//登陆前退出登陆
                    EMClient.getInstance().login(loginEntity.getData().getUsername(), loginEntity.getData().getPassword(), new EMCallBack() {
                        // 登录成功回调
                        @Override
                        public void onSuccess() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this, "环信登陆成功", Toast.LENGTH_SHORT).show();
                                    //将token存储
                                    SPUtils.getInstance().put("token",loginEntity.getData().getToken());


                                }
                            });

                        }

                        // 登录失败回调，包含错误信息
                        @Override
                        public void onError(final int code, final String error) {
                            Toast.makeText(LoginActivity.this, "环信登陆失败"+error, Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onProgress(int i, String s) {

                        }

                    });

                }
            }).start();
        }
    }

    /**
     * {
     *   "password": "string",
     *   "username": "string"
     * }
     * @param view
     */
    public void login(View view) {
        HashMap<String, String> map = new HashMap<>();
        map.put("username",etUsername.getText().toString());
        map.put("password",etPassword.getText().toString());
        String s = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), s);
        mPresenter.login(requestBody);
    }
}